#include "HttpUtil.h"
#include <QCoreApplication>
#include <QDebug>
#include <QThread>
#include <QDateTime>
#include "../config/ConfigManager.h"

// 测试用户信息路由
void testUserInfoRoute() {
    qDebug() << "\n=== 测试用户信息路由 ===";

    // 添加用户信息路由
    HttpUtil::addRoute(HttpUtil::Method::GET, "/user/info",
                       [](const QString &, const QMap<QString, QString> &query, const HttpUtil::json &) {
                           QString userId = query.value("id", "unknown");
                           return HttpUtil::json{
                               {"userId", userId.toStdString()},
                               {"username", "admin"},
                               {"role", "administrator"},
                               {"status", "active"}
                           };
                       });
}

// 测试设备更新路由
void testDeviceUpdateRoute() {
    qDebug() << "\n=== 测试设备更新路由 ===";

    // 添加设备更新路由
    HttpUtil::addRoute(HttpUtil::Method::POST, "/device/update",
                       [](const QString &, const QMap<QString, QString> &, const HttpUtil::json &body) {
                           // 验证必要字段
                           if (!body.contains("deviceId") || !body.contains("status")) {
                               return HttpUtil::json{
                                   {"success", false},
                                   {"error", "缺少必要字段"}
                               };
                           }

                           // 返回更新结果
                           return HttpUtil::json{
                               {"success", true},
                               {"deviceId", body["deviceId"]},
                               {"status", body["status"]},
                               {"updateTime", QDateTime::currentDateTime().toString(Qt::ISODate).toStdString()}
                           };
                       });
}

// 测试设备数据路由
void testDeviceDataRoute() {
    qDebug() << "\n=== 测试设备数据路由 ===";

    // 添加设备数据路由
    HttpUtil::addRoute(HttpUtil::Method::GET, "/device/data",
                       [](const QString &, const QMap<QString, QString> &query, const HttpUtil::json &) {
                           QString deviceId = query.value("deviceId", "unknown");
                           QString type = query.value("type", "all");

                           // 模拟设备数据
                           HttpUtil::json data = {
                               {"deviceId", deviceId.toStdString()},
                               {"type", type.toStdString()},
                               {
                                   "values", {
                                       {"temperature", 25.5},
                                       {"humidity", 60.0},
                                       {"pressure", 1013.2}
                                   }
                               },
                               {"timestamp", QDateTime::currentDateTime().toString(Qt::ISODate).toStdString()}
                           };

                           return data;
                       });
}

// 测试报警设置路由
void testAlarmRoute() {
    qDebug() << "\n=== 测试报警设置路由 ===";

    // 添加报警设置路由
    HttpUtil::addRoute(HttpUtil::Method::POST, "/alarm/set",
                       [](const QString &, const QMap<QString, QString> &, const HttpUtil::json &body) {
                           // 验证必要字段
                           if (!body.contains("deviceId") || !body.contains("threshold") || !body.contains("type")) {
                               return HttpUtil::json{
                                   {"success", false},
                                   {"error", "缺少必要字段"}
                               };
                           }

                           // 返回设置结果
                           return HttpUtil::json{
                               {"success", true},
                               {"alarmId", "alarm_" + std::to_string(QDateTime::currentMSecsSinceEpoch())},
                               {"deviceId", body["deviceId"]},
                               {"type", body["type"]},
                               {"threshold", body["threshold"]},
                               {"createTime", QDateTime::currentDateTime().toString(Qt::ISODate).toStdString()}
                           };
                       });
}

// 测试登录路由
void testLoginRoute() {
    qDebug() << "\n=== 测试登录路由 ===";

    // 添加登录路由
    HttpUtil::addRoute(HttpUtil::Method::POST, "/login",
                       [](const QString &, const QMap<QString, QString> &, const HttpUtil::json &body) {
                           // 从配置文件获取用户名和密码
                           QString configUsername = ConfigManager::instance().getString("user.username");
                           QString configPassword = ConfigManager::instance().getString("user.password");

                           // 验证用户名和密码
                           if (body["username"] == configUsername.toStdString() &&
                               body["password"] == configPassword.toStdString()) {
                               return HttpUtil::json{
                                   {"success", true},
                                   {"token", "test_token_" + std::to_string(QDateTime::currentMSecsSinceEpoch())},
                                   {
                                       "userInfo", {
                                           {"username", configUsername.toStdString()},
                                           {"role", "administrator"}
                                       }
                                   }
                               };
                           }

                           return HttpUtil::json{
                               {"success", false},
                               {"error", "用户名或密码错误"}
                           };
                       });
}

/**
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    // 初始化HttpUtil
    HttpUtil::getInstance().init();

    // 注册所有路由
    testLoginRoute();
    testUserInfoRoute();
    testDeviceUpdateRoute();
    testDeviceDataRoute();
    testAlarmRoute();

    qDebug() << "服务器启动成功，监听端口:" << ConfigManager::instance().getInt("server.port", 8080);

    return app.exec();
}
*/
